package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallCart;
import com.ctsi.ssdc.domain.LitemallCartExample;
import com.ctsi.ssdc.service.LitemallCartService;
import com.ctsi.ssdc.repository.LitemallCartRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallCart.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCartServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallCartRepository, LitemallCart, Integer, LitemallCartExample> 
	implements LitemallCartService {

    @Autowired
    LitemallCartRepository litemallCartRepository;


    /**
    * GET  /litemallCarts : get the litemallCarts firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallCart> findFirstStringColumn(String goodsSn,Pageable pageable){
        String str = goodsSn;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallCartExample litemallCartExample = new LitemallCartExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallCartExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallCartExample.createCriteria();
        } else{
            litemallCartExample.createCriteria().andGoodsSnLike("%" + str +"%");
        }
        List<LitemallCart>  data = litemallCartRepository.selectByExample(litemallCartExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallCartRepository.countByExample(litemallCartExample);
        }
        return new PageResult<LitemallCart>(data,count,count);
    }

    @Override
    public void deleteById(Integer id) {
        litemallCartRepository.logicalDeleteByPrimaryKey(id);

    }

    @Override
    public List<LitemallCart> queryByUid(Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return litemallCartRepository.selectByExample(example);
    }

    @Override
    public LitemallCart queryExist(Integer goodsId, Integer productId, Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andGoodsIdEqualTo(goodsId).andProductIdEqualTo(productId).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return litemallCartRepository.selectOneByExample(example);
    }

    @Override
    public void add(LitemallCart cart) {
        cart.setAddTime(ZonedDateTime.now());
        cart.setUpdateTime(ZonedDateTime.now());
        litemallCartRepository.insertSelective(cart);
    }

    @Override
    public int updateById(LitemallCart existCart) {
        existCart.setUpdateTime(ZonedDateTime.now());
        return litemallCartRepository.updateByPrimaryKeySelective(existCart);
    }

    @Override
    public LitemallCart findById(Integer cartId) {
        return litemallCartRepository.selectByPrimaryKey(cartId);
    }

    @Override
    public void clearGoods(Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true);
        LitemallCart cart = new LitemallCart();
        cart.setDeleted(true);
        litemallCartRepository.updateByExampleSelective(cart, example);
    }

    @Override
    public LitemallCart findById(Integer userId, Integer id) {

        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andIdEqualTo(id).andDeletedEqualTo(false);
        return litemallCartRepository.selectOneByExample(example);
    }

    @Override
    public int updateCheck(Integer userId, List<Integer> productIds, Boolean isChecked) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andProductIdIn(productIds).andDeletedEqualTo(false);
        LitemallCart cart = new LitemallCart();
        cart.setChecked(isChecked);
        cart.setUpdateTime(ZonedDateTime.now());
        return litemallCartRepository.updateByExampleSelective(cart, example);
    }

    @Override
    public int delete(List<Integer> productIdList, int userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andProductIdIn(productIdList);
        return litemallCartRepository.logicalDeleteByExample(example);
    }



    @Override
    public List<LitemallCart> queryByUidAndChecked(Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true).andDeletedEqualTo(false);
        return litemallCartRepository.selectByExample(example);
    }


    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
